home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16787 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.3 KB  |  65 lines

  1. Path: grimsel.zurich.ibm.com!usenet
  2. From: Keith Whittingham <wgk@zurich.ibm.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Virtual function does not make any sense unless it is pure.
  5. Date: Fri, 12 Apr 1996 11:02:43 -0700
  6. Organization: IBM Zurich Research Laboratory
  7. Message-ID: <316E9AC3.1124@zurich.ibm.com>
  8. References: <4kkhbc$nj4@brahms.udel.edu>
  9. NNTP-Posting-Host: pine.zurich.ibm.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.01 (Win16; I)
  14.  
  15. Yue-hong Zheng wrote:
  16. > Can any body give me any examples to show non-pure virtual function does
  17. > make sense?
  18.  
  19.  
  20. #include <iostream.h>
  21.  
  22. class Base
  23. {
  24.   public:
  25.     virtual const char *Name() { return "Base"; }
  26. };
  27.  
  28.  
  29. class Derived:
  30.    public Base
  31. {
  32.   public:                        // Ctors, dtors...
  33.     virtual const char *Name() { return "Derived"; }
  34. };
  35.  
  36.  
  37. #pragma argsused
  38. int main(
  39.   int     argc,
  40.   char    *argv[])
  41. {
  42.   int     DosRetCode = 0;
  43.   Base    b, *bp;
  44.   Derived d;
  45.  
  46.   bp = &b;
  47.   cout << bp->Name() << endl;
  48.   bp = &d;
  49.   cout << bp->Name() << endl;
  50.  
  51.  
  52.   return DosRetCode;
  53. }
  54.  
  55.  
  56. That help.
  57.  
  58. The "pure" in pure virtual is used exactly the same way but where
  59. you want to  force the user of a base class to implement a method
  60.  
  61. -- 
  62. Keith Whittingham
  63. wgk@zurich.ibm.com
  64.